package com.example.bookserver.controller;

import com.example.BOOKServer.bean.User;
import com.example.BOOKServer.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bookapp")
public class UserController {
    @Autowired
    UserDao userDao;
    Map<String,Object> map=new HashMap<>();
    //register
    @RequestMapping("/user/register")
    @ResponseBody
    public Map<String,Object>register(User user){
        User user1=userDao.save(user);
        if (user1!=null){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }

        return map;
    }
    @RequestMapping("/user/login")
    @ResponseBody
    public Map<String,Object>login(User user){
        User user1=userDao.findOne(Example.of(user)).orElse(null);
        if (user1!=null){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}